package com.dikeleakagedetection.myapplication


import android.content.Intent

import android.os.Bundle

import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar

import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment

import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.dikeleakagedetection.myapplication.databinding.ActMainBinding
import com.dikeleakagedetection.myapplication.ui.ble.BleFragment
import com.dikeleakagedetection.myapplication.ui.login.LoginActivity
import com.dikeleakagedetection.myapplication.utils.CheckPermissionsUtils
import com.dikeleakagedetection.myapplication.utils.showToast
import com.google.android.material.floatingactionbutton.FloatingActionButton


class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    private lateinit var binding: ActMainBinding

    private val fragmentManager by lazy {
        supportFragmentManager
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        //初始化视图
        initView()
        //动态权限检查
        CheckPermissionsUtils.checkPermissions(this)

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CheckPermissionsUtils.REQUEST_CODE_OPEN_GPS) {
            if (CheckPermissionsUtils.checkGPSIsOpen(this)) {
                "已打开GPS".showToast()
            }
        }
    }

    //初始化View
    private fun initView() {
        //ToolBar
        val toolbar: Toolbar = findViewById(R.id.tb_act_main)
        setSupportActionBar(toolbar)

        //NavView
        val navController = findNavController(R.id.nav_host_fragment)
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_new_project,
                R.id.nav_data_monitor,
                R.id.nav_database
            ),
            binding.drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        binding.nvActMain.setupWithNavController(navController)
        val drawerView = binding.nvActMain.inflateHeaderView(R.layout.nav_header_act_main)
        drawerView.setOnClickListener {
            val intent = Intent(this, LoginActivity::class.java)
            startActivity(intent)
        }

        //Fab
        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener {
            binding.drawerLayout.openDrawer(GravityCompat.END)
        }

        //BleFragment
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.frag_ble_act_main, BleFragment.newInstance()).commit()//替换这个fragment

    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

    fun replaceFragment(fragment: Fragment) {
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.frag_ble_act_main, fragment).commit()
    }
}